<?php

// get CDDBID and/or albId 
if (!isset($cddbid) && !isset($albId)) {
   if ($_REQUEST['albId']) {
      $albId = $_REQUEST['albId'];
   } else {
   	if ($_GET['cddbid'] ) {
	   	$albId = $cddbid = $_GET['cddbid'];
   	} elseif ($_POST['cddbid']) {
	   	$albId = $cddbid = $_POST['cddbid'];
	   } 
	}
}

if (($mode === 'cdedit') && (!$albId)) { //in order to edit, we've got to have something to edit!  Since we've chosen to edit a CD, but haven't chosen an album to make changes to, why not list them all here so we can choose one?  Yeah, I'm smart.
	echo('Select the CD you would like to edit<br />');
	$cd_list_sql = "SELECT `cddbid`,`title`,`artist` FROM `album` ";
	$cd_list_result = mysql_query($cd_list_sql);
	while($cd_list_row = mysql_fetch_assoc($cd_list_result)) {
		echo('<a href="'.$_SERVER['PHP_SELF'].'?mode='.$mode.'&cat='.$cat.'&cddbid='.$cd_list_row[cddbid].'">'.$cd_list_row[title].'</a> by '.$cd_list_row[artist].'<br />' );
	}
} elseif (($mode === 'cdedit') && (isset($albId)) && ($do === 'cdedit')) {

   // are we adding a new album?
   if ($albId === 'new') {
      // any new album has to have at least an album title or artist name! 
      if (!$_REQUEST[cdartist] && !$_REQUEST[album]) {
         echo ('<h3>You must enter an album title or artist to add a CD</h3><a href="?mode=cdedit&albId=new">try again</a>.');
         die();
      } else {
         mysql_query("INSERT INTO `album` 
            SET 
            `title`='".mysql_real_escape_string($_POST['cdtitle'])."'
            ");
         $albId = mysql_insert_id();
      }
   }
   //lets start by editing the album information (we'll edit the track information soon enough)
   //$update_album_sql = "UPDATE `` SET "
   	//$in_date =       $edit_date = date("Ymd"); // set the day we got this CD 
	//	$add_date = date('Y-m-d', strtotime(($_POST['add_year'] . $_POST['add_month'] . $_POST['add_day']));
	if ($_POST['status'] != $_POST['oldstatus']) {
		$alb[edit_date] = date("Y-m-d");
	} else {
		$alb[edit_date] = $_POST['edit_date'];
	}
		//$add_date = date("Y-m-d", mktime(0, 0, 0, $_POST['add_day'], $_POST['add_month'], $_POST['add_year']));
		$mysqlconnect = mysql_connect($DB_HOST, $DB_WRITE_USER, $DB_WRITE_USER_PASS);
		mysql_select_db($DB_NAME, $mysqlconnect);
		$album_update_sql = "UPDATE `album` 
			SET `slot`='".mysql_real_escape_string($_POST['slot'])."',
			`title`='".mysql_real_escape_string($_POST['album'])."',
			`artist`='".mysql_real_escape_string($_POST['cdartist'])."',
			`edit_date`='".mysql_real_escape_string($alb[edit_date])."',
			`in_date`='".mysql_real_escape_string($_POST['in_date'])."',
			`status`='".mysql_real_escape_string($_POST['status'])."',
			`review`='".mysql_real_escape_string($_POST['review'])."',
			`comments`='".mysql_real_escape_string($_POST['comments'])."',
			`add_date`='".mysql_real_escape_string($_POST['add_date'])."',
			`label`='".mysql_real_escape_string($_POST['label'])."',
			`genre`='".mysql_real_escape_string($_POST['genre'])."',
			`category`='".mysql_real_escape_string($_POST['category'])."',
			`category2`='".mysql_real_escape_string($_POST['category2'])."',
			`category3`='".mysql_real_escape_string($_POST['category3'])."',
			`promoter`='".mysql_real_escape_string($_POST['promoter'])."',
			`promoter2`='".mysql_real_escape_string($_POST['promoter2'])."',
			`promoter3`='".mysql_real_escape_string($_POST['promoter3'])."',
			`multi`='".mysql_real_escape_string($_POST['multi'])."',
			`is_comp`='".mysql_real_escape_string($_POST['is_comp'])."',
			`virtue`='".$_REQUEST['albVirtue']."',
			`multi_id`='".$_REQUEST['multi_id']."'
			WHERE `cddbid`='".mysql_real_escape_string($albId)."' ";
//			echo($album_update_sql);
   mysql_query($album_update_sql) or die(mysql_error()); 
}

?><pre><?php 

if ($_POST['trk']) {
   foreach ($_POST['trk'] as $trk) { 
      
      if ($trk[action] && $_REQUEST[trkAction] === 'rmTrk') {
         mysql_query(" DELETE FROM `track` WHERE `id`='".$trk[id]."' ");
      } else {
   //      echo ($trk[id].$trk[num].'. '.$trk[title].'('.$trk[artist].')'.$albId.'<br />');
         if (!$trk[artist]) $trk[artist] = $_POST['cdartist'];
         
         $checkFileQuery = mysql_query("SELECT `filepath`,`filename` FROM `track` WHERE `id`='".$trk[id]."' ");
         $fileArray = mysql_fetch_array($checkFileQuery);
         $trk[category] = $_REQUEST[category];
         $trk[status] = $_REQUEST[status];
         $trk[album] = $_REQUEST[album];
         $trk[filepathname] = $fileArray[filepath].$fileArray[filename];
         if ($trk[filepathname]) {
            $trkFile =mvTrkFile($trk[filepathname], $trk, $MUSIC_DIR);
         }
         if ($trk[id] === 'new' && ($trk[title] || $trk[filename] || $trk[filepath] || $trk[virtue]) ) {
            // insert new track into DB 
            mysql_query("INSERT INTO `track` SET `cddbid` = '".$albId."' ");
            $trk[id] = mysql_insert_id();
         }
         $trkUpdateQuery = " UPDATE `track` 
          SET
           `title` = '".mysql_real_escape_string($trk[title])."',
           `comment` = '".mysql_real_escape_string($trk[comment])."', 
           `artist` = '".mysql_real_escape_string($trk[artist])."',
           `virtue` = '".mysql_real_escape_string($trk[virtue])."',
           `trackno` = '".$trk[num]."',
           `time` = '".mysql_real_escape_string($trk[time])."' ";
         if ($trkFile) {
            $trkUpdateQuery .= ", 
            `filepath`='".mysql_real_escape_string($trkFile[filepath])."',
            `filename`='".mysql_real_escape_string($trkFile[filename])."'
            ";
         }
         $trkUpdateQuery .=  " WHERE (`id` = '".$trk[id]."') ";
         mysql_query($trkUpdateQuery) or die(mysql_error());
      }
   }
   
//   mysql_free_result($trkUpdate);
}

?></pre><?php 

// we may be able to remove the following 
//
// 
//if ($then_do === 'add_another_cd') {
//	unset($albId);
//} elseif (!$albId) {
//	$albId = mysql_insert_id();
//}
//
//
//

$mysqlconnect = mysql_connect($DB_HOST, $DB_VIEW_USER, $DB_VIEW_USER_PASS);
mysql_select_db($DB_NAME, $mysqlconnect);

if ($albId !== 'new') {
   /* get information fron the album database */
   $albQuery = mysql_query("
      SELECT * FROM album 
      WHERE cddbid='".mysql_real_escape_string($albId)."' 
      LIMIT 1");
   $alb = mysql_fetch_array($albQuery);
} else $alb = null;

// set variables from the gathered data
$oldstatus = $alb[status];
if ($alb[multi]) $multi = $alb[multi];
?>
<form name="albEdit" action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
<img src="./themes/<?=$theme?>/img/blank.png" onLoad="rateAlb(<?=$alb[virtue]?>);" alt="" />
<span id="albVirtue"><?php 
if (!$alb[virtue]) $$alb[virtue] = 3;
   echo('<input type="hidden" name="albVirtue" value="'.$alb[virtue].'" />');
   if ($albVirtue == 1) {
      echo('
      <img src="./themes/'.$theme.'/img/virtue1_on.png" onClick="rateAlb(1)" /><img src="./themes/'.$theme.'/img/virtue2_off.png" onClick="rateAlb(2)" /><img src="./themes/'.$theme.'/img/virtue3_off.png" onClick="rateAlb(3)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(4)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(5)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(6)" />');
   } else if ($alb[virtue] == 2) {
      echo('
      <img src="./themes/'.$theme.'/img/virtue1_off.png" onClick="rateAlb(1)" /><img src="./themes/'.$theme.'/img/virtue2_on.png" onClick="rateAlb(2)" /><img src="./themes/'.$theme.'/img/virtue3_off.png" onClick="rateAlb(3)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(4)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(5)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(6)" />');
   } else if ($alb[virtue] == 3) {
      echo('
      <img src="./themes/'.$theme.'/img/virtue1_off.png" onClick="rateAlb(1)" /><img src="./themes/'.$theme.'/img/virtue2_off.png" onClick="rateAlb(2)" /><img src="./themes/'.$theme.'/img/virtue3_on.png" onClick="rateAlb(3)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(4)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(5)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(6)" />');  
   } else if ($alb[virtue] == 4) {
      echo('
      <img src="./themes/'.$theme.'/img/virtue1_off.png" onClick="rateAlb(1)" /><img src="./themes/'.$theme.'/img/virtue2_off.png" onClick="rateAlb(2)" /><img src="./themes/'.$theme.'/img/virtue3_off.png" onClick="rateAlb(3)" /><img src="./themes/'.$theme.'/img/star_selected.png" onClick="rateAlb(4)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(5)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(6)" />');
   } else if ($alb[virtue] == 5) {
      echo('
      <img src="./themes/'.$theme.'/img/virtue1_off.png" onClick="rateAlb(1)" /><img src="./themes/'.$theme.'/img/virtue2_off.png" onClick="rateAlb(2)" /><img src="./themes/'.$theme.'/img/virtue3_off.png" onClick="rateAlb(3)" /><img src="./themes/'.$theme.'/img/star_selected.png" onClick="rateAlb(4)" /><img src="./themes/'.$theme.'/img/star_selected.png" onClick="rateAlb(5)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(6)" />');
   } else if ($alb[virtue] > 5) {
      echo('
      <img src="./themes/'.$theme.'/img/virtue1_off.png" onClick="rateAlb(1)" /><img src="./themes/'.$theme.'/img/virtue2_off.png" onClick="rateAlb(2)" /><img src="./themes/'.$theme.'/img/virtue3_off.png" onClick="rateAlb(3)" /><img src="./themes/'.$theme.'/img/star_selected.png" onClick="rateAlb(4)" /><img src="./themes/'.$theme.'/img/star_selected.png" onClick="rateAlb(5)" /><img src="./themes/'.$theme.'/img/star_selected.png" onClick="rateAlb(6)" />');
   } else {
      echo('
      <img src="./themes/'.$theme.'/img/virtue1_off.png" onClick="rateAlb(1)" /><img src="./themes/'.$theme.'/img/virtue2_off.png" onClick="rateAlb(2)" /><img src="./themes/'.$theme.'/img/virtue3_off.png" onClick="rateAlb(3)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(4)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(5)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(6)" />
      ');
   }
?></span> <?php 
if ($alb[edit_date]) {
	echo ('status of this album last changed on '.$alb[edit_date].'.');
}
?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="document.albEdit.submit()">[Save]</a><br />
<table>
<tr>
<td class="inputTitleLeft"><b>Artist</b></td>
<td style="width: 280px;"><input type="text" name="cdartist" value="<?=$alb[artist]?>" size="28"  tabindex="4"  style="width: 240px;" /></td>
<td rowspan="3"><b>Collection(s)</b><br />
<select name="category" tabindex="16">
<option value="">[Primary]</option>
<?php
$category_query = mysql_query("SELECT `id`,`category`,`default` FROM `category` ORDER BY `category`");
//$category_result = mysql_query($category_query);
while ($category_row = mysql_fetch_row($category_query)) {
?><option value="<?=$category_row[0]?>"<?php 
if ($category_row[0] === $alb[category]) {
	print(' selected="selected" ');
	$catSet = true;
} elseif ($category_row[2] && !$catSet){
print(' selected="selected"');
}
?>><?=$category_row[1]?></option>
<?php
}
?>
</select><br />
<select name="category2" tabindex="16">
<option value="">[Secondary]</option>
<?php
$category_query = "SELECT `id`,`category`,`default` FROM `category` ORDER BY `category`";
$category_result = mysql_query($category_query);
while ($category_row = mysql_fetch_row($category_result)) {
?><option value="<?=$category_row[0]?>"<?php 
if ($category_row[0] === $alb[category2]) {
	print(' selected="selected" ');
}
?>><?=$category_row[1]?></option>
<?php
}
?>
</select><br />
<select name="category3" tabindex="16">
<option value="">[Tertiary]</option>
<?php
$category_query = "SELECT `id`,`category`,`default` FROM `category` ORDER BY `category`";
$category_result = mysql_query($category_query);
while ($category_row = mysql_fetch_row($category_result)) {
?><option value="<?=$category_row[0]?>"<?php 
if ($category_row[0] === $alb[category3]) {
	print(' selected="selected" ');
}
?>><?=$category_row[1]?></option>
<?php
}
?>
</select><br /></td>
</tr><tr>
<td class="inputTitleLeft"><b>Title </b></td>
<td><input type="text" name="album" value="<?=$alb[title]?>" size="28"  tabindex="5"  style="width: 240px;"/><div class="helpInfo">(ID# <?php echo($albId)?>)</div></td>
</tr><tr>
<td class="inputTitleLeft"  title="<?php include('./widgets/slotCheck.inc'); ?>" ><b>Slot#</b></td>
<td><input type="hidden" name="albId" id="albId" value="<?=$albId?>" title="<?php include('./widgets/slotCheck.inc'); ?>" />
<input type="text" name="slot" size="4" maxlength="4" value="<?=$alb[slot]?>" tabindex="1">
<select name="multi" id="multi" tabindex="2" onChange="albSetMulti('<?=$alb[cddbid]?>');">
<option value="" <?php if (!$multi) echo(' selected="selected"')?>></option>
<option value="A" <?php if ($multi === 'A') echo (' selected="selected"') ?>>A</option>
<option value="B" <?php if ($multi === 'B') echo (' selected="selected"') ?>>B</option>
<option value="C" <?php if ($multi === 'C') echo (' selected="selected"') ?>>C</option>
<option value="D" <?php if ($multi === 'D') echo (' selected="selected"') ?>>D</option>
<option value="E" <?php if ($multi === 'E') echo (' selected="selected"') ?>>E</option>
<option value="F" <?php if ($multi === 'F') echo (' selected="selected"') ?>>F</option>
<option value="G" <?php if ($multi === 'G') echo (' selected="selected"') ?>>G</option>
<option value="H" <?php if ($multi === 'H') echo (' selected="selected"') ?>>H</option>
</select><span id="albMulti"><?php
if ($alb[multi_id]) {
   printf('<span class="inputTitleLeft"> Alt. ID#</span><input name="multi_id" value="%s" type="text" style="width: 50px;" name="multi_id" onMouseOver="parent.albSetMultiQuery(\'%s\')" />', $alb[multi_id], $alb[multi_id]);
}
?></span>
<input type="checkbox" onClick="toggleAlbComp();" id="is_comp" name="is_comp" value="1" tabindex="3" <?php if ($alb[is_comp]) echo(' checked="checked"') ?> /><b>comp.</b></td>
</tr>

<tr>
<td class="inputTitleLeft">Add-date
<input type="hidden" name="oldstatus" value="<?php echo($oldstatus) ?>" />
<input type="hidden" name="edit_date" value="<?=$alb[edit_date]?>" />
</td>

<td class="inputBox"><input type="text" id="add_date" name="add_date" value="<?=$alb[add_date]?>" 
tabindex="9" style="width: 110px; vertical-align: middle;" /><img src="./themes/<?=$theme?>/img/calendar-16.gif"  id="date_selector" />
<div class="helpInfo">(YYYY-MM-DD)</div><br /></td>

<td> </td>
</tr>

<tr>
<td class="inputTitleLeft">Label</td><td class="inputBox"><input type="text" name="label" value="<?=$alb[label]?>" tabindex="10" style="width: 190px;" /></td>

<td rowspan="3" class="inputTitle">Promoter(s)<br />
<select name="promoter" style="width: 180px;" tabindex="6">
<option value="" <?php
if (!$alb[promoter]) {
	echo (' selected="selected"');
}
?>>[Promoter 1]</option>
<?php
$promoSql = "SELECT `id`,`nickname`,`company` FROM `promoter` ORDER BY `company`";
$promoQuery = mysql_query($promoSql);
while ($promo = mysql_fetch_array($promoQuery)) {
?><option value="<?=$promo[id]?>" <?
if ($promo[id] === $alb[promoter]) {
	echo (' selected="selected"');
}
?>
><?=$promo[company]?> (<?=$promo[nickname]?>)</option>
<?php
}
?></select><br />
<select name="promoter2" style="width: 180px;" tabindex="7">
<option value="" <?php
if (!$alb[promoter2]) {
	echo (' selected="selected"');
}
?>>[Promoter 2]</option>
<?php
$promo2Sql = "SELECT `id`,`nickname`,`company` FROM `promoter` ORDER BY `company`";
$promo2Query = mysql_query($promo2Sql);
while ($promo2 = mysql_fetch_array($promo2Query)) {
?><option value="<?=$promo2[id]?>" <?
if ($promo2[id] === $alb[promoter2]) {
	echo (' selected="selected"');
}
?>
><?=$promo2[company]?> (<?=$promo2[nickname]?>)</option>
<?php
}
?></select><br />

<select name="promoter3" tabindex="8" style="width: 180px;">
<option value="" <?php
if (!$alb[promoter3]) {
	echo (' selected="selected"');
}
?>>[Promoter 3]</option>
<?php
$promo3Sql = "SELECT `id`,`nickname`,`company` FROM `promoter` ORDER BY `company`";
$promo3Query = mysql_query($promo3Sql);
while ($promo3 = mysql_fetch_array($promo3Query)) {
?><option value="<?=$promo3[id]?>" <?
if ($promo3[id] === $alb[promoter3]) {
	echo (' selected="selected"');
}
?>
><?=$promo3[company]?> (<?=$promo3[nickname]?>)</option>
<?php
}
?></select></td>
</tr>
<tr><td class="inputTitleLeft">Genre</td><td><input type="text" name="genre" value="<?=$alb[genre]?>" tabindex="11" style="width: 190px;" /></td>
</tr>
<tr>
   <td class="inputTitleLeft">Status</td>
   <td><?php
if (!isset($oldstatus)){
$oldstatus = 1;
}
?>
<select name="status" tabindex="13">
	<option value="0"<?php if ($oldstatus == 0) { 
	echo(' checked="checked"');
	} ?>>undatabased</option>
	<option value="1" <?php if ($oldstatus == 1) { 
	echo('selected');
	} ?>>in review</option>
	<option value="2" <?php if ($oldstatus == 2) { 
	echo('selected');
	} ?>>in rotation</option>
	<option value="3" <?php if ($oldstatus == 3) { 
	echo('selected');
	} ?>>old</option>
	<option value="4" <?php if ($oldstatus == 4) { 
	echo('selected');
	} ?>>archived</option>
	<option value="5" <?php if ($oldstatus == 5) { 
	echo('selected');
} ?>>discarded/lost</option></select></td>
</tr>
<tr>
<td class="inputTitleLeft">Description</td><td colspan="3"><textarea cols="60" rows="2" name="review" tabindex="14"><?=$alb[review]?></textarea></td>
</tr><tr>
<td class="inputTitleLeft"></td><td colspan="3"><textarea cols="60" rows="2" name="comments" tabindex="15" class="<?php
if (!$alb[comments]) echo 'hidden';
?>"><?=$alb[comments]?></textarea></td>
</tr>
</table>

<span style="width: 100%; text-align: right;"><input type="checkbox" id="trkExtInfo" checked="checked" onClick="toggleTrkExtInfo();" /> hide extended track info</span>
<br />
<table id="artistTable" style="padding-left: 20px;">
<tr style="text-align: center">
<th class="albEditTrkHead"><img src="./themes/<?=$theme?>/img/check.png" title="Click to check all tracks" /></th><th class="albEditTrkHead">#</th><th class="albEditTrkHead">Title</th><th class="albEditTrkHead"><span onClick="rateTrkAll(3)">Rating</span></th><th class="albEditTrkHead"><span id="artistHeading">Artist</span></th></tr>
<?php
/* get info from the tracks database */
if ($albId === 'new') {
   $track_result = null;
} else {
   $pre_tracksql = "SELECT * FROM `track` 
      WHERE `cddbid`='".mysql_real_escape_string($albId)."' 
      ORDER BY `trackno` ASC";
   $tracksql = $pre_tracksql; 
   $track_result = mysql_query($tracksql) or die(mysql_error());
   $numrows = mysql_num_rows($track_result);
}

if ($albId === 'new') {
   ?>
   <tr class="tracklist">
   <td class="tracklist">
   <input type="hidden" name="trk[1][id]" value="new" />
   1<input type="hidden" name="trk[1][num]" value="1" /></td>
   <td class="tracklist"><input id="trkTitle1" onBlur="blurAddTrk(1);" type="text" name="trk[1][title]" tabindex="<?php echo(16 + 1) ?>" />
	</td><td class="tracklist"><span id="rateTrk[1]">
	<img src="./themes/<?=$theme?>/img/virtue1_off.png" onClick="rateTrk(1,1)" /><img src="./themes/<?=$theme?>/img/virtue2_off.png" onClick="rateTrk(1,2)" /><img src="./themes/<?=$theme?>/img/virtue3_off.png" onClick="rateTrk(1,3)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(1,4)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(1,5)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(1,6)" /></span></td>
	<td class="tracklist"><span class="trkArtist<?php if (!$isComp) echo('Hidden')?>"><input id="trkArtist1" onBlur="blurAddTrk(1);" name="trk[1][artist]" type="text" /></span>
	</td>
	<?php //	||trkExtInfo[$trkRow]||<td></td><td>extended info</td><td></td><td></td>
	?>
	</tr>
	<?php 
} else {
$i = 0;
while ($i < ($numrows)) {
	$trk = $trackrow = mysql_fetch_array($track_result);
	$trackname[$i] = $trackrow[title];
	$virtue[$i] = $trackrow[virtue];
	$tcomment[$i] = $trackrow[comment];
	$tfilename[$i] = $trackrow[filepath].$trackrow[filename];
	$trackartist[$i] = $trackrow[artist];
	?>
	<tr class="tracklist">
	<td class="tracklist" style="text-align: left;"><input type="checkbox" name="trk[<?=$i?>][action]" value="<?=$trk[id]?>" /><input type="hidden" name="trk[<?=$i?>][id]" value="<?=$trk[id]?>" /><div onClick="rmTrk(<?=$trk[id]?>);"  style="display: inline; text-align: right;"><?php 
	if ($tfilename[$i]) {
	   if (file_exists($tfilename[$i])) {
	      ?><img width="13" height="13" title="a valid file exists for this track" alt="file icon" src="./themes/<?=$theme?>/img/fileIcon.png" <?php
	   } else {
	      ?><img width="13" height="13" title="the file for this track does not exist!" alt="broken file icon" src="./themes/<?=$theme?>/img/brokenFileIcon.png" <?php 
	   }
	} else {
	   ?><img src="./themes/<?=$theme?>/img/blank.png" <?php
	}
	?> /></div></td>
	<td class="tracklist" style="text-align: right;">
	<div id="trk[<?=$i?>][trkNum]" onClick="changeTrkNum(<?=$i?>, <?=$trk[trackno]?>)" title="click track number to change its value" style="display: inline; text-align: right;">
	<input type="hidden" name="trk[<?=$i?>][num]" value="<?=$trk[trackno]?>" /><?=$trk[trackno]?> 
	</div>
	</td>
	
	<td class="tracklist"><input type="text" name="trk[<?=$i?>][title]" value="<?=$trackname[$i]?>" tabindex="<?echo(16 + $i)?>" />
	</td>
	<td class="tracklist"><span id="rateTrk[<?=$i?>]"><script type="text/javascript">rateTrk(<?=$i?><?php if($trk[virtue]) echo (', '.$trk[virtue]); ?>);</script><?php 
	/* 
	switch ($virtue[$i]) {
	default:
	   ?><input type="hidden" name="trk[<?=$i?>][virtue]" value="2" /><img src="./themes/<?=$theme?>/img/virtue1_off.png" onClick="rateTrk(0,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/virtue2_off.png" onClick="rateTrk(1,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/virtue3_on.png" onClick="rateTrk(2,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(3,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(4,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(5,<?=$i?>)" />
	   <?php
	   break;
	case 0:
	   ?><input type="hidden" name="trk[<?=$i?>][virtue]" value="0" /><img src="./themes/<?=$theme?>/img/virtue1_on.png" onClick="rateTrk(0,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/virtue2_off.png" onClick="rateTrk(1,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/virtue3_off.png" onClick="rateTrk(2,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(3,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(4,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(5,<?=$i?>)" />
	   <?php
	   break;
	case 1:
	   ?><input type="hidden" name="trk[<?=$i?>][virtue]" value="1" /><img src="./themes/<?=$theme?>/img/virtue1_off.png" onClick="rateTrk(0,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/virtue2_on.png" onClick="rateTrk(1,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/virtue3_off.png" onClick="rateTrk(2,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(3,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(4,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(5,<?=$i?>)" />
	   <?php
	   break;
	case 2:
	   ?><input type="hidden" name="trk[<?=$i?>][virtue]" value="2" /><img src="./themes/<?=$theme?>/img/virtue1_off.png" onClick="rateTrk(0,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/virtue2_off.png" onClick="rateTrk(1,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/virtue3_on.png" onClick="rateTrk(2,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(3,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(4,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(5,<?=$i?>)" />
	   <?php
	   break;
	case 3:
	   ?><input type="hidden" name="trk[<?=$i?>][virtue]" value="3" /><img src="./themes/<?=$theme?>/img/virtue1_off.png" onClick="rateTrk(0,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/virtue2_off.png" onClick="rateTrk(1,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/virtue3_off.png" onClick="rateTrk(2,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_selected.png" onClick="rateTrk(3,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(4,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(5,<?=$i?>)" />
	   <?php
	   break;
	case 4:
	   ?><input type="hidden" name="trk[<?=$i?>][virtue]" value="4" /><img src="./themes/<?=$theme?>/img/virtue1_off.png" onClick="rateTrk(0,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/virtue2_off.png" onClick="rateTrk(1,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/virtue3_off.png" onClick="rateTrk(2,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_selected.png" onClick="rateTrk(3,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_selected.png" onClick="rateTrk(4,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(5,<?=$i?>)" />
	   <?php
	   break;
	case 5:
	   ?><input type="hidden" name="trk[<?=$i?>][virtue]" value="5" /><img src="./themes/<?=$theme?>/img/virtue1_off.png" onClick="rateTrk(0,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/virtue2_off.png" onClick="rateTrk(1,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/virtue3_off.png" onClick="rateTrk(2,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_selected.png" onClick="rateTrk(3,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_selected.png" onClick="rateTrk(4,<?=$i?>)" /><img src="./themes/<?=$theme?>/img/star_selected.png" onClick="rateTrk(5,<?=$i?>)" />
	   <?php
	   break;
	}
*/
	?></span>
	</td>
	<td class="tracklist"><span class="trkArtist"><input name="trk[<?=$i?>][artist]" value="<?php
	if($trackartist[$i]) echo($trackartist[$i]);
	else echo($alb[artist]);
	?>" type="text" /></span>
	</td>
	</tr><tr class="extInfo"><td> </td><td> </td><td colspan="8">
<input type="text" name="trk[<?=$i?>][comment]" value="<?=$tcomment[$i]?>" />
<input type="text" style="text-align: right;" name="trk[<?=$i?>][time]" value="<?php echo($trackrow[time]) ?>" size="5" maxlength="8"/><?=$tfilename[$i]?><!-- input type="file" name="tfilename[<?=$i?>]" value="<?=$tfilename[$i]?>" / --></td>
</tr><?php
	$i++;
}
} // end if ($albId !== 'new')
if ($track_result) mysql_free_result($track_result);
if ($albQuery) mysql_free_result($albQuery);
mysql_close($mysqlconnect);
?>
</table>
<?php
// if we don't reset the value of iterator $i when we have a new album, the count could be wrong. 
if ($albId === 'new') $i = 1;

?>
<div id="addTrkTrigger"><a onClick="addTrk('<?=($i+1)?>');">add track</a><input type="text" name="lastTrk" id="lastTrk" value="<?=$i?>" /></div>

<br />With checked: <select name="trkAction">
<option value="">Do nothing</option>
<option value="rmTrk">Delete</option>
<option value="mvTrk">Move</option>
</select>
<!-- input type="hidden" name="then_do" value="cdedit" / -->
<input type="hidden" name="do" value="cdedit" />
<input type="hidden" name="mode" value="cdedit" />
<input type="hidden" name="albId" value="<?=$albId?>" />
<input type="submit" value="Save Changes" tabindex="<?php echo(16 + $i++) ?>" >
<input type="reset" value="Cancel" tabindex="<?php echo(16 + $i++) ?>"  />
<br />
</form>

<script type="text/javascript">
//alert('test');
</script>

<script type="text/javascript">
<!-- //this is for the javascript calendar -- a beautiful GPL mini date selector courtesy of the folks at dynarch.com
Calendar.setup(
{
inputField : "add_date", // ID of the input field
ifFormat : "%Y-%m-%d", // the date format
button : "date_selector" // ID of the button
}
);
//end date selector stuff -->
</script>
<img alt="" onLoad="toggleAlbComp(); toggleTrkExtInfo(); albSetMulti('<?=$alb[cddbid]?>')" src="./themes/<?=$theme?>/img/blank.png" />
